﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace WowRaidListTracker.DAL
{
    public class CharacterSpecialization
    {
        public static List<CharacterSpecialization> CharacterSpecializationCollection = new List<CharacterSpecialization>();
        public static Dictionary<string, CharacterSpecialization> CharacterSpecializationDictionary = new Dictionary<string,CharacterSpecialization>();
        public static Dictionary<int, CharacterSpecialization> CharacterSpecializationById = new Dictionary<int, CharacterSpecialization>();

        public int Id
        {
            get;
            set;
        }

        public int ListTypeId
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public CharacterSpecialization()
        {

        }

        public CharacterSpecialization(int id, string name)
        {
            Id = id;
            Name = name;
        }

        public CharacterSpecialization(DataRow dr)
        {
            Load(dr);
        }

        public static void PopulateCharacterSpecializationCollection(ListTrackerDal dal)
        {
            CRUDReader qud = new CRUDReader();
            string sql = qud.get("CharacterSpecializations", CRUDReader.QUDType.Select);
            object executeResult = dal.Execute(sql, SQLiteQueryType.STANDARDQUERY);
            //CharacterSpecializationCollection = new List<CharacterSpecialization>();
            //CharacterSpecializationDictionary = new Dictionary<string, CharacterSpecialization>();
            //CharacterSpecializationById = new Dictionary<int, CharacterSpecialization>();

            CharacterSpecializationCollection.Add(new CharacterSpecialization()
            {
                Id = -1,
                Name = "Select One..."
            });
            if (typeof(DataTable) == executeResult.GetType())
            {
                DataTable result = executeResult as DataTable;
                foreach (DataRow dr in result.Rows)
                {
                    CharacterSpecialization cs = new CharacterSpecialization(dr);
                    CharacterSpecializationCollection.Add(cs);
                    CharacterSpecializationDictionary.Add(cs.Name, cs);
                    CharacterSpecializationById.Add(cs.Id, cs);
                }
            }
            else
            {
                throw new InvalidTypeException("The type returned by the ListTrackerDal.Execute method was not correct");
            }
        }

        private void Load(DataRow dr)
        {
            Id = Convert.ToInt32(dr["CharacterSpecializationId"]);
            Name = Convert.ToString(dr["CharacterSpecializationName"]);
            ListTypeId = Convert.ToInt32(dr["CharacterSpecializationRaidMemberListTypeId"]);
        }
    }
}
